﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using Controller.Enum;
using Model;
using Newtonsoft.Json.Linq;

namespace Controller.Helper
{
    public class SaleCo
    {
        public static string GetSaleClientBySaleCo(string saleCoUsername)
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                var sales = from salecoMapping in dataContext.tblSaleCoSaleMappings
                            join saleDetail in dataContext.tblUsers on salecoMapping.SaleUserName equals
                                saleDetail.UserName
                            where salecoMapping.SaleCoUserName.Equals(saleCoUsername) && saleDetail.IsDisable != true
                            select saleDetail;


                JObject k = JObject.FromObject(new
                {
                    AllSale = sales
                });
                return k.ToString();
            }

        }

        public static string GetAllSaleCo()
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                string SaleCoRoleName = WebConfigurationManager.AppSettings["SaleCoRoleName"] ?? "SaleCo";
                var sales = from salecos in dataContext.tblUsers
                            where salecos.RoleName.Equals(SaleCoRoleName) && salecos.IsDisable != true
                            select salecos;

                JObject k = JObject.FromObject(new
                {
                    AllSaleCo = sales
                });
                return k.ToString();
            }
        }

        public static string GetAllSaleCoSaleMapping()
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                var sales = from saleCosales in dataContext.tblSaleCoSaleMappings
                            join userDetail in dataContext.tblUsers on saleCosales.SaleCoUserName equals userDetail.UserName
                            where userDetail.IsDisable != true
                            select new
                            {
                                saleCosales.SaleCoUserName,
                                saleCosales.SaleUserName,
                                saleCoName = userDetail.FirstName + " " + userDetail.Lastname
                            };
                var data = from s in sales
                           join userdetail in dataContext.tblUsers on s.SaleUserName equals userdetail.UserName
                           
                           select new
                           {
                               s.SaleUserName,
                               s.SaleCoUserName,
                               s.saleCoName,
                               saleName = userdetail.FirstName + " " + userdetail.Lastname
                           };
                JObject k = JObject.FromObject(new
                {
                    AllSaleCoSale = data
                });
                return k.ToString();
            }
        }

        public static int SaleCoAction(List<tblSaleCoSaleMapping> salecoSaleMappings, EnumAction action)
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                if (salecoSaleMappings != null && salecoSaleMappings.Count > 0)
                {
                    switch (action)
                    {
                        case EnumAction.Update:
                            try
                            {
                                foreach (tblSaleCoSaleMapping newaSalecoMapping in salecoSaleMappings)
                                {
                                    tblSaleCoSaleMapping oldsalecoMapping = dataContext.tblSaleCoSaleMappings.Single(salecoMapping => salecoMapping.SaleCoUserName == newaSalecoMapping.SaleCoUserName && salecoMapping.SaleUserName == newaSalecoMapping.SaleUserName);
                                    oldsalecoMapping.isDisable = newaSalecoMapping.isDisable;
                                    oldsalecoMapping.Comment = newaSalecoMapping.Comment;
                                }
                                dataContext.SubmitChanges();
                                return salecoSaleMappings.Count;
                            }
                            catch (Exception ex)
                            {

                                return -1;
                            }
                        case EnumAction.Insert:
                            try
                            {
                                var existingFoos = from exits in dataContext.tblSaleCoSaleMappings
                                                   select exits;
                                var saleToInsert = salecoSaleMappings.Where(newFoo => !existingFoos.Any(existingFoo => newFoo.SaleCoUserName == existingFoo.SaleCoUserName && newFoo.SaleUserName == existingFoo.SaleUserName));
                                int update = saleToInsert.Count();
                                int result = 0;
                                if (update > 0)
                                {
                                    dataContext.tblSaleCoSaleMappings.InsertAllOnSubmit(saleToInsert);
                                    dataContext.SubmitChanges();
                                    return update;
                                }
                                else
                                {
                                    dataContext.ExecuteCommand("delete from tblSaleCoSaleMapping");
                                    dataContext.tblSaleCoSaleMappings.InsertAllOnSubmit(salecoSaleMappings);
                                    dataContext.SubmitChanges();
                                    return salecoSaleMappings.Count;
                                }
                                
                            }
                            catch (Exception ex)
                            {
                                return -1;
                            }
                        case EnumAction.Delete:
                            try
                            {
                                var salecoMapping = from newsaleCoMapping in dataContext.tblSaleCoSaleMappings
                                              where salecoSaleMappings.Contains(newsaleCoMapping)
                                              select newsaleCoMapping;
                                //dataContext.tblUsers.DeleteByPK<tblUser>(users.AsQueryable());
                                int deletedSalecoMapping = dataContext.tblSaleCoSaleMappings.DeleteBatch<tblSaleCoSaleMapping>(salecoMapping);
                                //dataContext.tblUsers.UpdateBatch<tblUser>(users1, p => new tblUser{ Email = p.DateCreated, 
                                //           UserName = "Chris Cavanagh" } );
                                //dataContext.tblUsers.DeleteAllOnSubmit(users);
                                //dataContext.SubmitChanges();
                                return deletedSalecoMapping;
                            }
                            catch (Exception)
                            {

                                return -1;
                            }
                    }
                }
                else
                {
                    return 0;
                }
            }
            return 0;
        }
    
    }
}
